#include "mqueue.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char const *argv[])
{
    struct mq_attr attr = {
        .mq_maxmsg = 10,   /* 队列最大消息数 */
        .mq_msgsize = 128, /* 消息大小 */
    };

    /* 1. 创建消息队列 */
    mqd_t mqd = mq_open("/my_mq", O_CREAT | O_RDWR, 0666, &attr); // 队列名称一定要以/开头
    if (mqd == -1)
    {
        perror("mq_open");
        return -1;
    }

    /* 2. 发送消息 */
    mq_send(mqd, "hello world", strlen("hello world"), 0);
    if (mq_send(mqd, "hello world", strlen("hello world"), 0) == -1)
    {
        perror("mq_send");
        return -1;
    }

    /* 3. 接收消息 */
    char buf[256];
    ssize_t bytes_received = mq_receive(mqd, buf, sizeof(buf), NULL);
    if (bytes_received == -1)
    {
        perror("mq_receive");
        exit(1);
    }

    printf("Received message: %s\n", buf);

    /* 4. 关闭、删除消息队列 */
    mq_close(mqd);
    return 0;
}
